package com.rds.workflow.controller;

import com.rds.common.core.controller.BaseController;
import com.rds.common.core.domain.AjaxResult;
import com.rds.common.core.page.TableDataInfo;
import com.rds.common.utils.DateUtils;
import com.rds.common.utils.SecurityUtils;
import com.rds.common.utils.uuid.SnowFlakeUtils;
import com.rds.workflow.domain.RdaWfNodeAgentRel;
import com.rds.workflow.service.IRdaWfNodeAgentRelService;
import com.rds.workflow.utils.WorkflowStateid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pms/workflowNodeAgent")
public class RdaWfNodeAgentRelController extends BaseController {

    @Autowired
    private IRdaWfNodeAgentRelService rdaWfNodeAgentRelService;
    /**
     * 查询节点委托
     */
    @PreAuthorize("@ss.hasPermi('pms:workflowNodeAgent:agentNodeList')")
    @GetMapping("/agentNodeList")
    public TableDataInfo agentNodeList(RdaWfNodeAgentRel rdaWfNodeAgentRel){
        startPage();
        rdaWfNodeAgentRelService.updateRdaWfNodeAgentRelByStateid(WorkflowStateid.agentNodeObsoleted,WorkflowStateid.agentNodeCreateid,WorkflowStateid.agentNodeActivation);
        return getDataTable(rdaWfNodeAgentRelService.selectRdaWfNodeAgentRelList(rdaWfNodeAgentRel));
    }

    /**
     * 查询流程节点委托详情
     */
    @PreAuthorize("@ss.hasPermi('pms:workflowNodeAgent:agentNodeList')")
    @GetMapping("/agentNodeList/{id}")
    public AjaxResult agentNodeList(@PathVariable("id") Long id){
        return AjaxResult.success(rdaWfNodeAgentRelService.selectRdaWfNodeAgentRelByid(id));
    }

    /**
     * 新增流程节点委托
     */
    @PreAuthorize("@ss.hasPermi('pms:workflowNodeAgent:addWorkflowNodeAgent')")
    @PostMapping("/addWorkflowNodeAgent")
    @Transactional
    public AjaxResult addWorkflowNodeAgent(@RequestBody RdaWfNodeAgentRel rdaWfNodeAgentRel){
        rdaWfNodeAgentRel.setId(SnowFlakeUtils.nextId());
        rdaWfNodeAgentRel.setOwnerid(SecurityUtils.getUserId());
        rdaWfNodeAgentRel.setTypeid(WorkflowStateid.agentNodeTypeid);
        rdaWfNodeAgentRel.setPolicyid(WorkflowStateid.agentNodePolicyid);
        rdaWfNodeAgentRel.setStateid(WorkflowStateid.agentNodeCreateid);
        rdaWfNodeAgentRel.setCreateTime(DateUtils.getNowDate());
        return toAjax(rdaWfNodeAgentRelService.insertRdaWfNodeAgentRel(rdaWfNodeAgentRel));
    }

    /**
     * 修改流程节点委托
     */
    @PreAuthorize("@ss.hasPermi('pms:workflowNodeAgent:agentNodeList')")
    @PutMapping("/agentNodeList")
    @Transactional
    public AjaxResult updateNodeAgent(@RequestBody RdaWfNodeAgentRel rdaWfNodeAgentRel){
        rdaWfNodeAgentRel.setModifyTime(DateUtils.getNowDate());
        return toAjax(rdaWfNodeAgentRelService.updateRdaWfNodeAgentRel(rdaWfNodeAgentRel));
    }
    /**
    * 删除流程节点委托
     */
    @PreAuthorize("@ss.hasPermi('pms:workflowAgent:deleteWorkflowNodeAgent')")
    @DeleteMapping("/deleteWorkflowNodeAgent/{ids}")
    @Transactional
    public AjaxResult deleteWorkflowNodeAgent(@PathVariable Long[] ids){
        return toAjax(rdaWfNodeAgentRelService.deleteRdaWfNodeAgentRelByIds(ids));
    }
}
